package com.lx.commonlib.utils;

import android.util.Log;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
*  类说明：线程工具类
*  created by liuxiong on 2017/7/25
**/
public class ThreadUtil {
	private static final String TAG="xxxxxxx"+ThreadUtil.class.getSimpleName();

	//线程池执行者的代理对象
	private static ThreadPoolProxy threadPoolProxy;

    private ThreadUtil(){}

	//取得代理对象
	public static ThreadPoolProxy getInstance(){
		if(threadPoolProxy==null){
			threadPoolProxy=new ThreadPoolProxy(3,5,5L);
		}
		return threadPoolProxy;
		
	}
	//线程池执行者的代理类
	public static class ThreadPoolProxy{
		private int corePoolSize;
		private int maximumPoolSize;
		private long keepAliveTime;

		/**
		* 方法说明:  构造方法
		* @param  corePoolSize 核心线程数量
		* @param  maximumPoolSize 最大线程数量
		* @param  keepAliveTime  无任务时的活跃时间
		*@author liuxiong
		*@time 2017/7/20 16:36
		*/
		public ThreadPoolProxy(int corePoolSize,int maximumPoolSize,long keepAliveTime){
			this.corePoolSize = corePoolSize;
			this.maximumPoolSize = maximumPoolSize;
			this.keepAliveTime = keepAliveTime;
		}
		//java 的线程池的执行者，管理者
		private ThreadPoolExecutor poolExecutor;

		/**
		* 方法说明:  执行任务
		* @param  r 可执行任务
		*@author liuxiong
		*@time 2017/7/25 10:15
		*/
		public void execute(Runnable r){
			if(poolExecutor==null||poolExecutor.isShutdown()){
				poolExecutor=new ThreadPoolExecutor(
						//核心线程数量
						corePoolSize,
						//最大线程数量
						maximumPoolSize,
						// 在keepAliveTime时间内，没有执行任务，也是活跃的，超过该时间，则处于停止状态
						keepAliveTime,
						//时间单元，毫秒
						TimeUnit.MILLISECONDS,
						//任务队列
						new LinkedBlockingDeque<Runnable>(),
						//线程工厂  ,缺省线程工厂
						Executors.defaultThreadFactory());
				Log.d(TAG,"new ThreadPoolExecutor");
			}
			poolExecutor.execute(r);
		}

		/**
		* 方法说明:  停止执行新的任务,将正在执行的任务和等待中的任务都执行完了再关闭
		* created by liuxiong on 2019/3/21 15:52
		*/
		public void shutdown(){
			if(poolExecutor!=null&&(!poolExecutor.isShutdown())){
				poolExecutor.shutdown();
			}else{
				Log.d(TAG,"poolExecutor为空或已停止");
			}
		}

		/**
		* 方法说明:  停止执行新的任务，将正在执行的任务完成后，返回等待中的任务
		* @return  等待中的任务集合
		* created by liuxiong on 10:54
		*/
		public List<Runnable> shutdownNow(){
            List<Runnable> runnables=null;
            if(poolExecutor!=null&&(!poolExecutor.isShutdown())){
                runnables = poolExecutor.shutdownNow();
            } else{
				Log.d(TAG,"poolExecutor为空或已停止");
			}
			return runnables;
		}

		/**
		* 方法说明:  移除正在等待执行的某个任务（已经开始执行的任务不能执行）
		* @param  runnable 要移除的任务
		* @return 是否成功移除
		* created by liuxiong on 2019/3/22 10:58
		*/
		public boolean remove(Runnable runnable){
			if(runnable==null){
				throw new NullPointerException("XXX任务为空");
			}

            boolean remove=false;

            if(poolExecutor!=null&&(!poolExecutor.isShutdown())){
                remove = poolExecutor.remove(runnable);
            }else{
				Log.d(TAG,"poolExecutor为空或已停止");
			}

			return remove;
		}
	}
}